package main

import (
	_ "CSCredis/config" //导入此包 该包的init函数才会实现调用
	"CSCredis/tcp"
)

/**
 * @Author: hejing@szlabsun.com
 * @Description:
 * @File:  main
 * @Version: 1.0.0
 * @Date: 2021/6/24 17:54
 */
func main() {
	_ = tcp.ListenWithSignal()
	//nums := []int{0,0,1,0,1,1,0,1,1,0,0}
	//element := numSubarraysWithSum(nums,2)
	//fmt.Println(element)
}

//func search(nums []int, target int) int {
//	left := len(nums) / 2
//	if target >= nums[left] {
//		for target >= nums[left] {
//			left = left / 2
//		}
//	}
//	right := len(nums)
//	if target < nums[left] {
//		for target < nums[left] {
//			left = left / 2
//		}
//	}
//
//}

func movingCount(m int, n int, k int) (ans int) {
	cache := make([][]int, m)
	res := make([][]int, m)
	for i := range cache {
		cache[i] = make([]int, n)
	}
	for i := range res {
		res[i] = make([]int, n)
	}
	var backtracking func(i int, j int)
	var calculate func(i int, j int) int
	backtracking = func(i int, j int) {
		// 数组越界，或者已经访问过，以及不能进入此格子则直接return
		if i < 0 || j < 0 || i >= m || j >= n || cache[i][j] == 1 || res[i][j] == 1 || calculate(i, j) > k {
			return
		}
		cache[i][j] = 1
		res[i][j] = 1
		backtracking(i-1, j)
		backtracking(i+1, j)
		backtracking(i, j-1)
		backtracking(i, j+1)
		cache[i][j] = 0
	}
	calculate = func(i int, j int) (ans int) {
		ans += (i % 10)
		for i >= 10 {
			i = i / 10
			ans += (i % 10)
		}
		ans += (j % 10)
		for j >= 10 {
			j = j / 10
			ans += (j % 10)
		}
		return
	}
	backtracking(0, 0)
	for _, v := range res {
		for _, v := range v {
			if v == 1 {
				ans++
			}
		}
	}
	return
}

func numSubarraysWithSum(nums []int, goal int) int {
	premi, sum, res := map[int]int{}, 0, 0
	for _, num := range nums {
		premi[sum]++
		sum += num
		res += premi[sum-goal]
	}
	return res
}

func majorityElement(nums []int) int {
	current, count := 0, 0
	for i := range nums {
		if count == 0 {
			current = nums[i]
			count++
		} else if current == nums[i] {
			count++
		} else {
			count--
		}
	}
	if count == 0 {
		return -1
	}
	count = 0
	for i := range nums {
		if current == nums[i] {
			count++
		}
	}
	if count*2 > len(nums) {
		return current
	} else {
		return -1
	}
}
